package com.weishuang.account.manager.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.weishuang.account.manager.RoleManager;
import com.weishuang.model.account.dto.AccountUserLoginDTO;
import com.weishuang.account.entity.User;
import com.weishuang.account.manager.UserManager;
import com.weishuang.account.service.UserService;
import com.weishuang.account.utils.PasswordUtil;
import com.weishuang.model.account.dto.RoleDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserManagerImpl implements UserManager {

    @Autowired
    private UserService userService;

    @Override
    public void login(AccountUserLoginDTO req) {
        //生成密码
        String password = PasswordUtil.generatePassword(req.getPassword());

        User user = userService.getOne(req.getUserName(), password);
        if (user == null) {
            throw new RuntimeException("账号或密码错误");
        }

        StpUtil.login(user.getId());
        StpUtil.getSession().set("USER_DATA", user);
    }

    @Override
    public void register(AccountUserLoginDTO req) {
        User user = new User();
        user.setUserName(req.getUserName());
        //生成密码
        user.setPassword(PasswordUtil.generatePassword(req.getPassword()));

        userService.save(user);
    }
}
